﻿using Caliburn.Micro;
using MusicExplorer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
using MusicExplorer.Tools;
using MusicExplorer.Extensions;

namespace MusicExplorer.ViewModels.Wrappers
{
    public sealed class QueryGroupTileViewModel : PropertyChangedBase
    {
        private readonly QueryGroup m_group;
        private readonly CategoryWrapper m_parent;

        private QueryGroupTileViewModel(CategoryWrapper parent, QueryGroup group)
        {
            m_parent = parent;
            m_group = group;
            m_repositoryIcons = new List<BitmapImage>();
        }

        public static async Task<QueryGroupTileViewModel> CreateAsync(CategoryWrapper parent, QueryGroup group)
        {
            var wrapper = new QueryGroupTileViewModel(parent, group);
            await wrapper.InitializeAsync();
            return wrapper;
        }

        private async Task InitializeAsync()
        {
            await LoadImage();
            await LoadRepositoryIcons();
        }

        private async Task LoadImage()
        {
            m_image = new BitmapImage();
            switch (Size)
            {
                case QueryGroupSize.Small:
                    m_image.DecodePixelHeight = 170;
                    break;
                case QueryGroupSize.Medium:
                    m_image.DecodePixelWidth = 330;
                    break;
                case QueryGroupSize.Large:
                    m_image.DecodePixelHeight = 330;
                    break;
            }
            await m_image.FillWithBytes(m_group.Image);
        }

        private async Task LoadRepositoryIcons()
        {
            m_repositoryIcons.Clear();
            foreach(var repository in m_group.Entities.Select(e => e.Repository).GroupBy(r => r.Id))
            {
                var icon = new BitmapImage();
                await icon.FillWithIconRepositoryAsync(repository.First());
                m_repositoryIcons.Add(icon);
            }
        }

        public QueryGroup Group { get { return m_group; } }
        public CategoryWrapper Parent { get { return m_parent; } }
        public string DisplayName { get { return m_group.Name; } }

        public QueryGroupSize Size
        {
            get { return m_group.Size; }
            set
            {
                if (m_group.Size != value)
                {
                    m_group.Size = value;
                    NotifyOfPropertyChange(() => Size);
                    NotifyOfPropertyChange(() => Image);
                    Parent.Parent.SaveNeeded = true;
                }
            }
        }

        public async Task ReloadImage()
        {
            await LoadImage();
            NotifyOfPropertyChange(() => Image);
        }

        private BitmapImage m_image;
        public BitmapImage Image { get { return m_image; } }

        private readonly List<BitmapImage> m_repositoryIcons;
        public List<BitmapImage> RepositoryIcons { get { return m_repositoryIcons; } }

        private bool m_isSelected;
        public bool IsSelected
        {
            get { return m_isSelected; }
            set
            {
                if(m_isSelected != value)
                {
                    m_isSelected = value;
                    NotifyOfPropertyChange(() => IsSelected);
                    Parent.RefreshSelectedGroup();
                }
            }
        }
    }
}
